package com.wechat.order.util.xmlUtils;

import lombok.extern.slf4j.Slf4j;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 残缺代码，不准备再次写
 * 解析xml
 * Created by zzy on 2018/1/1.
 */
@Slf4j
public class SAXxmlUtil {

    private List<Map<String,Object>> resultList=new ArrayList<>();

    /**
     * 读取xml
     * @param inputStream
     * @return
     * @throws Exception
     */
    public static  List<Map<String,Object>> readXML(InputStream inputStream) throws Exception {

        SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
        SAXParser saxParser=saxParserFactory.newSAXParser();
        SAXParserHandler saxParserHandler=new SAXParserHandler();
        saxParser.parse(inputStream,saxParserHandler);
        return null;
    }

    /**
     *
     */
    public static void createXml() throws Exception {
        //1、创建TransformerFactory
        SAXTransformerFactory transformerFactory= (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        //2、通过创建TransformerFactory创建transformerHandler
        TransformerHandler transformerHandler= transformerFactory.newTransformerHandler();
        //3、通过handler创建tranformer
        Transformer transformer=transformerHandler.getTransformer();
        //4、设置属性
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");//字符集
        transformer.setOutputProperty(OutputKeys.INDENT,"true");//换行
        //5、创建一个result
        String parentPath=  System.getProperty("user.dir");
        String path=parentPath+"\\src\\main\\resources\\xml\\student3.xml";
        File file= new File(path);
        if(!file.exists()){
            file.createNewFile();
        }
        Result result=new StreamResult(new FileOutputStream(file));
        transformerHandler.setResult(result);
        //7、编写xml文件内容
        transformerHandler.startDocument();

        AttributesImpl attributes=new AttributesImpl();
        attributes.clear();
        attributes.addAttribute("","","sex","","男");
        transformerHandler.startElement("","","zzy",attributes);
        transformerHandler.characters("zzy".toCharArray(),0,3);
        transformerHandler.endElement("","","zzy");
        transformerHandler.endDocument();
    }
}
@Slf4j
class SAXParserHandler extends DefaultHandler{

    /**
     * 用来遍历xml的开始标签
     * @param uri
     * @param localName
     * @param qName
     * @param attributes
     * @throws SAXException
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);

        log.info("SAX解析开始===:"+qName);
    }

    /**
     * 用来遍历xml的结束标签
     * @param uri
     * @param localName
     * @param qName
     * @throws SAXException
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        log.info("SAX解析结束:===="+qName);
    }

    /**
     * 用来标识解析开始
     * @throws SAXException
     */
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
    }

    /**
     * 用来表示解析结束
     * @throws SAXException
     */
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }
}
